package com.wyd.jwpkglxt_ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.jwpkglxt_ssm.dao.PtDepartmentDao;
import com.wyd.jwpkglxt_ssm.entity.Department;
import com.wyd.jwpkglxt_ssm.entity.ProfessionalTitle;
import com.wyd.jwpkglxt_ssm.dao.ProfessionalTitleDao;
import com.wyd.jwpkglxt_ssm.entity.PtDepartment;
import com.wyd.jwpkglxt_ssm.service.ProfessionalTitleService;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import com.wyd.jwpkglxt_ssm.vo.ProfessionalTitleVo;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * (ProfessionalTitle)表服务实现类
 *
 * @author wyd
 * @since 2021-08-07 21:26:54
 */
@Service("professionalTitleService")
public class ProfessionalTitleServiceImpl implements ProfessionalTitleService {
    @Autowired
    private ProfessionalTitleDao professionalTitleDao;

    @Autowired
    private PtDepartmentDao ptDepartmentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProfessionalTitle queryById(Integer id) {
        return this.professionalTitleDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ProfessionalTitle> queryAllByLimit(int offset, int limit) {
        return this.professionalTitleDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param professionalTitleVo 视图对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(ProfessionalTitleVo professionalTitleVo) {
        ProfessionalTitle professionalTitle = new ProfessionalTitle();
        professionalTitle.setRealName(professionalTitleVo.getRealName()).setTitle(professionalTitleVo.getTitle());
        int insertProfessionalTitle = this.professionalTitleDao.insert(professionalTitle.setCreateTime(new Timestamp(System.currentTimeMillis())));
        if(insertProfessionalTitle!=0){
            List<ProfessionalTitle> professionalTitles = this.professionalTitleDao.queryAll(professionalTitle.setCreateTime(null));
            if (!professionalTitles.isEmpty()){
                PtDepartment ptDepartment = new PtDepartment();
                ptDepartment.setProfessionalTitleId(professionalTitles.get(0).getId()).setDepartmentId(professionalTitleVo.getDid()).setCreateTime(new Timestamp(System.currentTimeMillis()));
                int insertPtDepartment = this.ptDepartmentDao.insert(ptDepartment);
                if(insertPtDepartment!=0){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param professionalTitleVo 视图对象
     * @return 实例对象
     */
    @Override
    public Boolean update(ProfessionalTitleVo professionalTitleVo) {
        ProfessionalTitle professionalTitle = new ProfessionalTitle();
        professionalTitle.setId(professionalTitleVo.getId()).setRealName(professionalTitleVo.getRealName()).setTitle(professionalTitleVo.getTitle());
        int updateProfessionalTitle = this.professionalTitleDao.update(professionalTitle.setUpdateTime(new Timestamp(System.currentTimeMillis())));
        if(updateProfessionalTitle!=0){
            PtDepartment ptDepartment = new PtDepartment();
            ptDepartment.setId(professionalTitleVo.getPtDepartmentId()).setDepartmentId(professionalTitleVo.getDid()).setUpdateTime(new Timestamp(System.currentTimeMillis()));
            int updatePtDepartment = this.ptDepartmentDao.update(ptDepartment);
            if(updatePtDepartment!=0){
                return true;
            }
        }
        return false;
    }



    @Override
    public PageInfo<ProfessionalTitleVo> queryAllByPage(Integer pageNum, Integer pageSize, ProfessionalTitleVo professionalTitleVo) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProfessionalTitleVo> professionalTitleVos = this.professionalTitleDao.queryAllWithProfessionalTitleVo(professionalTitleVo);
        return new PageInfo<>(professionalTitleVos);
    }

    @Override
    public Boolean deleteProfessionalTitle(String ids) {
        if(ids.contains("-")){
            String[] idsplit = ids.split("-");
            List<Integer> professionalTitleIds = new ArrayList<>();
            for(String s : idsplit){
                professionalTitleIds.add(Integer.parseInt(s));
            }
            int delProfessionalTitles = this.professionalTitleDao.deleteProfessionalTitleBatch(professionalTitleIds);
            if(delProfessionalTitles!=0){
                return true;
            }
        }else if(!"".equals(ids)){
            Integer id = Integer.parseInt(ids);
            int delProfessionalTitle = this.professionalTitleDao.deleteById(id);
            if(delProfessionalTitle!=0){
                return true;
            }
        }

        return false;
    }

    @Override
    public List<ProfessionalTitleVo> queryAllWithProfessionalTitleVo(ProfessionalTitleVo professionalTitleVo) {
        return this.professionalTitleDao.queryAllWithProfessionalTitleVo(professionalTitleVo);
    }
}